﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class VINDAL
    {
        #region 获取机型相应的配置信息
        public static IDataReader VinMac_GetAll()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VinMac_GetAll");
            return DB.ExecuteReader(dbCommand);
        }
        #endregion

        #region 获取其它配置信息
        public static IDataReader VinConfig_GetAllByType(int ConfigType)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VinConfig_GetAllByType", ConfigType);
            return DB.ExecuteReader(dbCommand);
        }
        #endregion

        public static int Add(VinHistoryInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VinHistory_Add");
            DB.AddInParameter(dbCommand, "MacType", DbType.String, obj.MacType);
            DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
            DB.AddInParameter(dbCommand, "VIN", DbType.String, obj.VIN);
            DB.AddInParameter(dbCommand, "XXGKH", DbType.String, obj.XXGKH);
            DB.AddInParameter(dbCommand, "SCCDZ", DbType.String, obj.SCCDZ);
            DB.AddInParameter(dbCommand, "FDJH", DbType.String, obj.FDJH);
            DB.AddInParameter(dbCommand, "SCDATE", DbType.String, obj.SCDATE);
            DB.AddInParameter(dbCommand, "CCDATE", DbType.String, obj.CCDATE);
            DB.AddInParameter(dbCommand, "GKWWW", DbType.String, obj.GKWWW);
            DB.AddInParameter(dbCommand, "FDJSB", DbType.String, obj.FDJSB);
            DB.AddInParameter(dbCommand, "FDJSCCDZ", DbType.String, obj.FDJSCCDZ);

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        /// <summary>
        /// 返回－1表示机号不存在VIN，1为成功
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Update(VinHistoryInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VinHistory_Update");
            DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
            DB.AddInParameter(dbCommand, "FDJH", DbType.String, obj.FDJH);
            DB.AddInParameter(dbCommand, "SCDATE", DbType.String, obj.SCDATE);

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        public static bool Delete(int id)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VinHistory_Delete");
            DB.AddInParameter(dbCommand, "id", DbType.Int32, id);
         

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return DB.ExecuteNonQuery(dbCommand)>0;
        }
        public static IDataReader GetList(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VinHistory_GetList", ID);
            return DB.ExecuteReader(dbCommand);
        }
        public static IDataReader GetListByVin(string VIN)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VinHistory_GetListByVin", VIN);
            return DB.ExecuteReader(dbCommand);
        }
        public static IDataReader GetListBySer(string Ser)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VinHistory_GetListBySer", Ser);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTable GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VinHistory_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String, whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
    }
}
